package com.demo.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.demo.bean.PageBean;
import com.demo.bean.ResultBean;
import com.demo.model.User;
import com.demo.service.UserService;
import com.demo.util.CommonUtil;

@Controller("userAction")
@Scope("prototype")
public class UserAction extends BaseAction<ResultBean> {
	@Autowired
	private UserService userService;

	private Integer id;
	
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	public void getUserInfo() {
		User user=userService.getInfoById(id);
		Map<String,Object> result =new HashMap<String,Object>();
		result.put("obj", user);
		CommonUtil.sendMessage(result);
	}
	
	public void getUserList() {
		Map<String,Object> params =new HashMap<String,Object>();
		if(page==null) {
			page=1;
		}
		if(rows==null){
			rows=10;
		}
		params.put("page", page);
		params.put("rows", rows);
		PageBean obj=userService.getListByParams(params);
		Map<String,Object> result =new HashMap<String,Object>();
		result.put("objs", obj.getList());
		CommonUtil.sendMessage(result);
	}
	
	public void deleteUser() {
		int i=userService.delete(id+"");
		Map<String,Object> result =new HashMap<String,Object>();
		result.put("code", i);
		CommonUtil.sendMessage(result);
	}
	
	public void createUser() {
		User u=new User();
		u.setPassword("111");
		u.setUsername("111");
		userService.save(u);
		Map<String,Object> result =new HashMap<String,Object>();
		CommonUtil.sendMessage(result);
	}
	
	public void updateUser() {
		User u=userService.getInfoById(id);
		if(u!=null) {
			u.setPassword("111");
			userService.update(u);
		}
		Map<String,Object> result =new HashMap<String,Object>();
		CommonUtil.sendMessage(result);
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
}
